package com.ihighest.app.infox.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

public class ImageListviewAdapter extends BaseAdapter {
	ArrayList<String> imagelist;
	Context context;
	private final LayoutInflater inflater;

	public ImageListviewAdapter(Context context, ArrayList<String> imagelist) {
		super();
		this.imagelist = imagelist;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	public ImageListviewAdapter(Context context, String[] imageArr) {
		super();
		this.imagelist = new ArrayList<>();
		if (imageArr != null && imageArr.length > 0) {
			for (String image : imageArr) {
				this.imagelist.add(image);
			}
		}
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return imagelist != null ? imagelist.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return imagelist.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ViewHolder holder;
		if (arg1 == null) {
			holder = new ViewHolder();
			arg1 = inflater.inflate(R.layout.infox_imagelistview_itemlayout, null);
			holder.image = (ImageView) arg1.findViewById(R.id.id_questionimage);
			arg1.setTag(holder);
		} else {
			holder = (ViewHolder) arg1.getTag();
		}
		// DisplayImageOptions defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
		// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
		// .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
		// ImageLoader.getInstance().displayImage(imagelist.get(arg0),
		// holder.image, defaultOptions);
		//
		AppKit.SetNormalImg(imagelist.get(arg0), holder.image);

		holder.image.setTag(R.id.tag_1, imagelist.get(arg0));
		holder.image.setTag(R.id.tag_2, imagelist);
		holder.image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// String url = (String) arg0.getTag(R.id.tag_1);
				ArrayList<String> urls = (ArrayList<String>) arg0.getTag(R.id.tag_2);
				if (urls != null) {
					AppKit.showBigImageView(context, urls);
				}

				// if (!TextUtils.isEmpty(url)) {
				// AppKit.showBigImageView(context, url);
				// }
			}
		});
		return arg1;
	}

	class ViewHolder {

		ImageView image;

	}

}
